package com.leetcode;

public class P0468ValidateIpAddress {

	
	public static void main(String[] args) {
//		System.out.println(validIPAddress("172.16.254.1"));
//		System.out.println(validIPAddress("2001:0db8:85a3:0:0:8A2E:0370:7334"));
//		System.out.println(validIPAddress("256.256.256.256"));
		System.out.println(validIPAddress("1e1.4.5.6"));
	}
	
    public static String validIPAddress(String IP) {
        
    	
    	String[] ips = IP.split(":");
    	int len = ips.length;
    	if(len == 8) {
    		//ipv6
    		int sum = 0;
    		for(int i = 0 ; i < IP.length() ; i ++) {
    			if(IP.charAt(i) == ':') {
    				sum ++;
    			}
    		}
    		if(sum != 7 ) {
    			return "Neither";
    		}

    		for(String item : ips) {
    			item = item.toLowerCase();
    			if(item.isEmpty()) {
    				return "Neither";
    			}
    			if(item.length() == 4 && item.compareTo("ffff") > 0) {
    				return "Neither";
    			} else if(item.length() == 3 && item.compareTo("fff") > 0) {
    				return "Neither";
    			} else if(item.length() == 2 && item.compareTo("ff") > 0) {
    				return "Neither";
    			} else if(item.length() == 1 && item.compareTo("f") > 0) {
    				return "Neither";
    			} else if(item.length() == 0) {
    				return "Neither";
    			}
    			
    		}
    		return "IPv6";
    		
    	} else {
    		//ipv4
    		ips = IP.split("\\.");
    		int sum = 0;
    		for(int i = 0 ; i < IP.length() ; i ++) {
    			if(IP.charAt(i) == '.') {
    				sum ++;
    			}
    		}
    		if(sum != 3 ) {
    			return "Neither";
    		}
    		for(String item : ips) {
    			if(item.isEmpty() ||item.charAt(0) == '0') {
    				return "Neither";
    			}
    			if(item.length() == 2) {
    				item = "0" + item;
    			} else if(item.length() == 1) {
    				item = "00" + item;
    			}
    			if(item.compareTo("255") > 0) {
    				return "Neither";
    			}
    		}
    		return "IPv4";
    	}
    	
    }

}
